package com.qxzn.app.medicalhealth.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qxzn.app.bean.MedicineRemindDetailListBean;
import com.qxzn.app.medicalhealth.R;
import com.qxzn.app.medicalhealth.R2;
import com.qxzn.app.util.DateUtil;
import com.qxzn.app.util.DisplayUtil;
import com.qxzn.app.util.DrawableUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RemindDetailAdapter extends RecyclerView.Adapter<RemindDetailAdapter.ViewHolder> {

    private Context context;
    private List<MedicineRemindDetailListBean> dataList;
    private MedicineRemindDetailListBean item;

    public RemindDetailAdapter(Context context, List<MedicineRemindDetailListBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_remind_detail, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        item = dataList.get(position);
        RemindDetailSubAdapter adapter = new RemindDetailSubAdapter(context, item.getMedicineBean());
        holder.lrvSubMedicine.setAdapter(adapter);
        if (position == getItemCount() - 1) {
            holder.view1.setVisibility(View.INVISIBLE);
        } else {
            holder.view1.setVisibility(View.VISIBLE);
        }
        holder.tvTime.setText(item.getTime());
        String todayReminderTimeStr = DateUtil.formMillisToStr(System.currentTimeMillis(), DateUtil.yearToDatePattern1) + item.getTime();
        Long todayReminderTime = DateUtil.parseStrToMillis(todayReminderTimeStr, DateUtil.yearToDatePattern1 + DateUtil.hourToMinPattern);
        if (todayReminderTime != null && todayReminderTime < System.currentTimeMillis()) {
            holder.tvTime.setBackground(DrawableUtil.getShapeDrawable(context.getResources().getColor(R.color.gray_A6A6A6), DisplayUtil.dip2px(context, 3)));
            holder.view1.setBackgroundColor(context.getResources().getColor(R.color.gray_A6A6A6));
        } else {
            holder.tvTime.setBackground(DrawableUtil.getShapeDrawable(context.getResources().getColor(R.color.cyan_00DDE5), DisplayUtil.dip2px(context, 3)));
            holder.view1.setBackgroundColor(context.getResources().getColor(R.color.cyan_00DDE5));
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.tv_time)
        TextView tvTime;
        @BindView(R2.id.view1)
        View view1;
        @BindView(R2.id.lrv_sub_medicine)
        RecyclerView lrvSubMedicine;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            lrvSubMedicine.setLayoutManager(new GridLayoutManager(view.getContext(), 2));
        }
    }

}
